from fastapi import APIRouter, HTTPException
from typing import Optional
from pydantic import BaseModel

# setting
from settings.settings import settings
# agv socket 工厂方法
from sockets.agvsocket import AgvSocket

agv_sio_manager = AgvSocket()
nav_sio = agv_sio_manager.get(settings.AGV_PORT_NAVIGATE)


# 创建status router路由
navigate_router = APIRouter()


class StatusParams(BaseModel):
    msg_type: int
    msg: Optional[dict]

    class Config:
        schema_extra = {
            "example": {
                "msg_type": 1000,
                "msg": {"参数名称": "xx参数"}
            }
        }


@navigate_router.get("/", response_description="编号 查询结果")
async def get_navigate(msg_type: int):
    try:
        agv_sio_manager.send(nav_sio, 1, msg_type)
        res = agv_sio_manager.receive(nav_sio)
        return res
    except Exception as e:
        print(e)
        raise HTTPException(
            status_code=409,
            detail="查询编号不正确"
        )


@navigate_router.post("/", response_description="编号 查询结果")
async def post_navigate(params: StatusParams):
    try:
        agv_sio_manager.send(nav_sio, 1, params.msg_type, params.msg)
        res = agv_sio_manager.receive(nav_sio)
        return res
    except Exception as e:
        print(e)
        raise HTTPException(
            status_code=409,
            detail="查询编号不正确"
        )
